package com.grm.distributed.lock.mapper;

import com.grm.distributed.lock.pojo.Product;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ProductMapper {

    @Update("update product set count = #{count} where id = #{id}")
    void update(Product product);

    @Select("select * from product where id = #{id}")
    Product queryById(Long id);

    @Update("update product set count = count - 1 where id = #{id} and count > 0")
    void sell(@Param("id")Long id);

    @Select("select * from product where code = #{code} for update")
    List<Product> query(@Param("code") String code);

    @Select("select * from product where code = #{code}")
    List<Product> queryCAS(@Param("code") String code);

    @Update("update product set count = count - 1 ,version = version + 1 where id = #{id} and version = #{version}")
    Integer updateForCAS(@Param("id")Long id,@Param("version")Integer version);
}
